//
// Created by Ukua on 2024/1/100.
//

#include "BSP_buzzer.h"
#include "main.h"
#include "fifo.h"
#include "bsp_delay.h"


osThreadId Buzzer_MainLoopTaskHandle;
fifo_t *music_fifo;
uint16_t scale[]={D_Do,D_Re,D_Mi,D_Fa,D_Sol,D_La,D_Si,Do,Re,Mi,Fa,Sol,La,Si,H_Do,H_Re,H_Mi,H_Fa,H_Sol,H_La,H_Si,NO_SOUND};
//--------------------乐谱------------------
const uint16_t test_sound[]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};//测试基础音
const uint16_t test_time[]={7,7,7,5,5,5,7,7,7,5,5,5,5,5,5,5,5,5,5,5,5};
const uint16_t boot_sound[]={1, 1, 5, 5, 6, 6, 5, 4, 4, 3, 3, 2, 2, 1};
const uint16_t boot_time[]={2, 2, 2, 2, 2, 2, 4, 2, 2, 2, 2, 2, 2, 4};

//小燕子
const uint16_t xiaoyanzi_sound[]={3,5,8,6,5,100,//音调
                                  3,5,6,8,5,100,
                                  8,10,9,8,9,8,6,8,5,100,
                                  3,5,6,5,6,8,9,5,6,100,
                                  3,2,1,2,100,
                                  2,2,3,5,5,8,2,3,5,100};
const uint16_t xiaoyanzi_time[]={2,2,2,2,6,4,//时间
                                 2,2,2,2,6,4,
                                 6,2,4,4,2,2,2,2,6,4,
                                 6,2,4,2,2,4,2,2,6,4,
                                 2,2,4,6,4,
                                 4,2,2,4,4,4,2,2,6,4};


//生日快乐
const uint16_t shengrikuaile_sound[]={4,4,5,4,
                                      7,6,100,
                                      4,4,5,4,
                                      8,7,100,
                                      4,4,11,9,
                                      7,6,5,100,
                                      3,3,9,7,
                                      8,7,100,
};
const uint16_t shengrikuaile_time[] = {2,2,4,4,        //时间--2代表半拍(100ms) 4代表一拍(200ms) 8代表两拍(400ms)
                                       4,4,32,
                                       2,2,4,4,
                                       4,4,4,
                                       2,2,4,4,
                                       4,4,4,4,
                                       2,2,4,4,
                                       4,4,4,
};
//世间美好与你环环相扣
const uint16_t sjmhynhhxk_sound[]={11,11,11,10,9,9,8,8,7,8,
                                   9,9,11,11,9,8,8,9,9,
                                   7,7,7,7,7,9,9,9,8,8,8,7,8,
                                   9,9,9,11,8,100,
                                   11,11,11,11,10,9,8,7,8,
                                   9,9,11,11,9,8,8,7,7,
                                   7,7,7,7,7,9,9,9,8,8,8,7,8,
                                   9,11,11,100,100,9,
                                   8,100,100,4,5,4,
                                   9,9,12,9,10,9,9,7,8,9,8,8,7,7,
                                   7,7,9,7,8,7,7,5,6,7,6,6,
                                   9,9,9,4,5,4,
                                   9,9,9,9,11,9,9,7,8,9,8,8,7,7,
                                   7,7,9,9,9,7,5,6,6,7,6,6,
                                   5,7,8,8,7,7,8,8,9,8,
                                   7,7,7,100,100
};
const uint16_t sjmhynhhxk_time[] = {2,2,2,1,1,2,2,2,1,1,        //时间--2代表半拍(100ms) 4代表一拍(200ms) 8代表两拍(400ms)
                                    1,2,1,2,1,1,2,2,4,
                                    1,1,1,1,2,1,1,2,2,1,1,1,1,
                                    3,1,2,2,4,4,
                                    1,2,1,2,1,1,6,1,1,
                                    1,2,1,2,1,1,2,2,4,
                                    1,1,1,1,2,1,1,2,2,1,1,1,1,
                                    2,2,4,4,2,2,
                                    4,4,2,2,2,2,
                                    1,1,1,1,1,1,1,1,1,2,1,2,1,1,
                                    1,1,1,1,1,1,1,1,2,1,1,4,
                                    3,1,6,2,2,2,
                                    1,1,1,1,1,1,1,1,2,1,1,2,1,1,
                                    1,1,1,1,1,1,1,1,2,1,1,4,
                                    2,1,1,2,2,2,1,1,2,2,
                                    2,2,4,4,4,
};
//溯
const uint16_t su_sound[]={
        H_1,H_5,H_3,H_2,H_1,H_1,H_5,H_3,NONE,
        H_1,H_5,H_3,H_2,M_7,M_7,M_7,H_5,H_3,H_2,H_1,
        H_1,H_5,H_3,H_2,H_1,H_1,H_1,H_5,H_3,
        H_1,H_5,H_3,H_2,M_7,M_7,M_7,H_5,H_3,H_2,H_1,
};
const uint16_t su_time[]={
        2,2,1,2,1,2,2,4,4,
        2,2,1,2,1,1,1,2,1,2,1,
        2,2,1,2,1,1,1,2,4,
        2,2,1,2,1,1,1,2,1,2,1,
};
//别看我只是一只羊
const uint16_t xiyangyang_sound[]={
        M_1,NONE,

};
const uint16_t xiyangyang_time[]={
        2,8,

};
//--------------------乐谱------------------

void Sound(uint16_t frq)
{
    uint32_t time;

    if(frq!=NO_SOUND){
        time = 600000/frq;
        HAL_GPIO_WritePin(BUZZER_GPIO_Port,BUZZER_Pin,1);
        delay_us(time);
        HAL_GPIO_WritePin(BUZZER_GPIO_Port,BUZZER_Pin,0);
        delay_us(time);
    }else{
        delay_us(100);
    }

}
static void PlayMusic(Music *music){
    fifo_put(music_fifo,music);
}

static void Buzzer_PlayMusic_TASK()
{
    osDelay(3000);
    while (1){
        if(fifo_is_empty(music_fifo)){
//            osDelay(10);
            osThreadSuspend(NULL);
            continue;
        }
        Music now_music;
        if(fifo_get(music_fifo, &now_music)==0) {
            for (uint16_t i = 0; i < now_music.size; i++) {
                for (uint16_t e = 0; e < (now_music.time[i] * scale[now_music.sound[i]]) / now_music.speed; e++) {
                    Sound((uint32_t) scale[now_music.sound[i]]);
                }

            }
        }
    }
}

void BSP_Buzzer_Init()
{
    music_fifo= fifo_create(sizeof(Music),16);
    osThreadDef(Buzzer_MainLoopTask, Buzzer_PlayMusic_TASK, osPriorityLow, 0, 128);
    Buzzer_MainLoopTaskHandle = osThreadCreate(osThread(Buzzer_MainLoopTask), NULL);
    Music boot_music={
            .sound=boot_sound,
            .time=boot_time,
            .size=14,
            .speed=50,
    };
    PlayMusic(&boot_music);
}
